package com.woniuxy.project115.kaer.platform.controller;
import com.woniuxy.project115.kaer.commons.util.Result;
import com.woniuxy.project115.kaer.platform.annotation.RequirePermission;
import com.woniuxy.project115.kaer.platform.annotation.RequireRole;

import com.woniuxy.project115.kaer.platform.dto.RolePermissionDto;
import com.woniuxy.project115.kaer.platform.entity.Permission;
import com.woniuxy.project115.kaer.platform.service.RolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.javassist.expr.NewExpr;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fyy
 * @since 2025年02月23日
 */
@RestController
@RequestMapping("/rolePermission")
@Api(tags = "角色权限管理")
public class RolePermissionController {
    @Resource
    private RolePermissionService rolePermissionService;

    //给角色分配权限接口
    @PostMapping("/addRolePermission")
    @ApiOperation("给角色分配权限接口")
    @RequireRole("超级管理员")
    @RequirePermission("/rolePermission/addRolePermission")
    public Result addRolePermission(Integer roleId, Integer permissionId) throws Exception{
        rolePermissionService.addRolePermission(roleId, permissionId);
        return Result.succeed("添加角色权限成功", null);
    }

    //移除角色权限接口
    @PostMapping("/removeRolePermission")
    @ApiOperation("移除角色权限接口")
    @RequireRole("超级管理员")
    @RequirePermission("/rolePermission/removeRolePermission")
    public Result removeRolePermission(Integer roleId, Integer permissionId) throws Exception{
        rolePermissionService.removeRolePermission(roleId, permissionId);
        return Result.succeed("移除角色权限成功", null);
    }

    //查看该角色已有的权限资源
    @GetMapping("getRolePermission/{roleId}")
    @ApiOperation("查看该角色已有的权限资源")
    @RequireRole("超级管理员")
    @RequirePermission("查看当前角色所有的权限")
    public Result getRolePermission(@PathVariable Integer roleId) throws Exception {
        System.out.println("roleId="+roleId);
        final List<Permission> rolePermission = rolePermissionService.getRolePermission(roleId);
        return Result.succeed("获取该角色已有的权限成功",rolePermission);
    }

    //更新角色权限接口
    @PostMapping("/saveRolePermission/{roleId}")
    @ApiOperation("更新角色权限接口")
    @RequireRole("超级管理员")
    @RequirePermission("更新角色权限")
    public Result saveRolePermission(@PathVariable Long roleId, @RequestBody RolePermissionDto rolePermissionDto) throws Exception {
            // 调用服务层保存角色权限
            rolePermissionService.saveRolePermissions(rolePermissionDto);
            return Result.succeed("保存权限成功", null);
    }


}

